source('code/analysis/load.R')

data = read_dta('data/working/defund-persuasion.dta') %>%
  rename(policy = increase_funding) %>% 
  mutate(exp = 'defund') %>% 
  bind_rows(read_dta('data/working/deport-persuasion.dta') %>% mutate(exp = 'deport', sample = 'lucid') %>% rename(policy = deport)) %>% 
  bind_rows(read_dta('data/working/tucker-persuasion-prolific.dta') %>% mutate(exp = 'deport', sample = 'prolific') %>% rename(policy = deport)) %>% 
  data_finalize_persuasion()

main_results = function(data) {
  
  if (data$exp[1] == 'defund') {
    title = 'Persuasive effects of anti-defunding article'
    label = 'defund-persuasion'
  
  model1 = lm(posterior~info, data=data)
  model2 = lm(posterior~info+age+I(age^2)+race+hisp+male+education, data=data)
  model3 = lm(policy~info, data=data)
  model4 = lm(policy~info+age+I(age^2)+race+hisp+male+education, data=data)

  mean1 = formatC(mean(data$posterior[data$info==0], na.rm=T), digits=3, format='f')
  mean2 = formatC(mean(data$policy[data$info==0], na.rm=T), digits=3, format='f')

  out = stargazer(list(model1, model2, model3, model4), 
                  omit='age|race|hisp|male|rep|education|Constant|partisan|ind',
                  covariate.labels = c('Provided article'),
                  column.labels = c('\\textit{Belief}', '\\textit{Policy preference}'),
                  column.separate = c(2,2),
                  keep.stat = c('rsq', 'n'), 
                  title = title, label = str_interp('t:${label}'),
                  add.lines = list(c('Controls', rep(c('No', 'Yes'), 2))))
  
  means = str_interp('No Article mean & ${mean1} & ${mean1} & ${mean2} & ${mean2} \\\\')

  out = c(out[1:8], out[13], out[12], out[14:19], means, out[20:length(out)])
  out = star_notes_tex(out, note.type = 'threeparttable', note = tablenotes[[label]])
  
  }
  
  else if (data$exp[1] == 'deport') {
    title = 'Persuasive effects of \\textit{Tucker Carlson Tonight} video'
    label = 'deport-persuasion'

    # lucid
    model1 = lm(posterior~info, data=data %>% filter(sample == 'lucid'))
    model2 = lm(posterior~info+age+I(age^2)+race+hisp+male+education, data=data %>% filter(sample == 'lucid'))
    model3 = lm(policy~info, data=data %>% filter(sample == 'lucid'))
    model4 = lm(policy~info+age+I(age^2)+race+hisp+male+education, data=data %>% filter(sample == 'lucid'))

    mean1 = formatC(mean(data$posterior[data$info==0 & data$sample == 'lucid'], na.rm=T), digits=3, format='f')
    mean2 = formatC(mean(data$policy[data$info==0 & data$sample == 'lucid'], na.rm=T), digits=3, format='f')

    # prolific
    model5 = lm(posterior~info, data=data %>% filter(sample == 'prolific'))
    model6 = lm(posterior~info+age+I(age^2)+race+hisp+male+education, data=data %>% filter(sample == 'prolific'))
    model7 = lm(policy~info, data=data %>% filter(sample == 'prolific'))
    model8 = lm(policy~info+age+I(age^2)+race+hisp+male+education, data=data %>% filter(sample == 'prolific'))

    mean3 = formatC(mean(data$posterior[data$info==0 & data$sample == 'prolific'], na.rm=T), digits=3, format='f')
    mean4 = formatC(mean(data$policy[data$info==0 & data$sample == 'prolific'], na.rm=T), digits=3, format='f')

    out1 = stargazer(list(model1, model2, model3, model4), 
                    omit='age|race|hisp|male|rep|education|Constant|partisan|ind',
                    covariate.labels = c('Provided article'),
                    column.labels = c('\\textit{Belief}', '\\textit{Policy preference}'),
                    column.separate = c(2,2),
                    keep.stat = c('rsq', 'n'), 
                    title = title, label = str_interp('t:${label}'),
                    add.lines = list(c('Controls', rep(c('No', 'Yes'), 2))))
    
    out2 = stargazer(list(model5, model6, model7, model8), 
                     omit='age|race|hisp|male|rep|education|Constant|partisan|ind',
                     covariate.labels = c('Provided article'),
                     column.labels = c('\\textit{Belief}', '\\textit{Policy preference}'),
                     column.separate = c(2,2),
                     keep.stat = c('rsq', 'n'), 
                     title = title, label = str_interp('t:${label}'),
                     add.lines = list(c('Controls', rep(c('No', 'Yes'), 2))))

    means1 = str_interp('No Article mean & ${mean1} & ${mean1} & ${mean2} & ${mean2} \\\\')

    means2 = str_interp('No Article mean & ${mean3} & ${mean3} & ${mean4} & ${mean4} \\\\')

    out = c(out2[1:8], out2[13], out2[12], out2[14:15],
            '\\textbf{Panel A:} & \\multicolumn{4}{c}{\\textit{Prolific Sample}} \\\\ \\midrule',
            out2[16:19], means2, out2[21:22], '\\midrule',
            '\\textbf{Panel B:} & \\multicolumn{4}{c}{\\textit{Lucid Sample}} \\\\ \\midrule',
            out1[16:19], means1, out1[21:22], '\\midrule', out1[20], out1[23:length(out1)])
    out = star_notes_tex(out, note.type = 'threeparttable', note = tablenotes[[label]])
    
  }
  
  writeLines(out, con = str_interp('output/tables/${label}.tex'))
  
  
}

main_results(data %>% filter(exp=='defund'))
main_results(data %>% filter(exp=='deport'))


